// This sample program demonstrates how to create customized loggers.
package main

import (
	"io"
	"io/ioutil"
	"log"
	"os"
)

var ( //MARK 这里*表示是指针类型
	Trace   log.Logger  // Just about anything
	Info    *log.Logger // Important information
	Warning *log.Logger // Be concerned
	Error   *log.Logger // Critical problem
)

func init() {

	file, err := os.OpenFile("errors.txt", //MARK   *File实现了io.Writer 接口 (File实现了*File就实现了,除非要*io.Writer给File报错)
		os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalln("Failed to open error log file:", err)
	}
	Error = log.New(io.MultiWriter(file, os.Stderr), //MARK 输出到文件 并且 输出到 标准错误
		"ERROR: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	//MARK log.New 是一个工厂方法, 返回一个指针, 说明里面不是原始类型
	Trace = *log.New(ioutil.Discard, //MARK 加上* 把指针转成值 这里的ioutil.Discard 是丢弃, 这是用来禁用log的
		"TRACE: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	Info = log.New(os.Stdout, //MARK 第一个参数是 io.Writer 接口: 这里 指向标准输出的文件描述符 类型是 *File,*File实现了 io.Writer 接口
		"INFO: ",
		log.Ldate|log.Ltime|log.Lshortfile)

	Warning = log.New(os.Stdout,
		"WARNING: ",
		log.Ldate|log.Ltime|log.Lshortfile)

}

func main() {
	Trace.Println("I have something standard to say") //MARK 方法生命要求指针 我给值也行 因为会自动转
	Info.Println("Special Information")
	Warning.Println("There is something you need to know about")
	Error.Println("Something has failed")
}
